import React from "react";
import SearchTable from "components/searchTable/SearchTable.jsx";
import { openNotificationWithIcon } from "utils/comm";
import { connect } from "dva";
import ReturnBackForm from "../../../../components/flowdataDetail/ReturnBackForm";
import Base from "./Base";

const emptyParams = {
  modelName: "",
  serialNum: "",
};
@connect(({ FlowHandle, loading, appListMenu, Public }) => ({
  applyData: FlowHandle.applyData,
  focusApp: appListMenu.focusApp,
  loading,
  menuDataList: Public.menuDataList,
  resourceList: Public.resourceList,
}))
export default class MyApply extends Base {
  constructor(props) {
    super(props);
    this.openNotificationWithIcon = openNotificationWithIcon.bind(this);
    this.searchParams = emptyParams;

    this.state = {
      page: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  }

  componentWillMount() {
    this.initData();
  }

  componentDidUpdate(preProps) {
    const { focusApp } = this.props;
    const { focusApp: preFocusApp } = preProps;
    if (preFocusApp.id && `${preFocusApp.id || 0}` !== `${focusApp.id || 0}`) {
      // 应用切换更新页面数据
      this.resetPageData();
    }
  }

  // 撤回弹窗
  handleReturnBackVisible = (visible, id) => {
    this.setState({ returnBackVisible: visible, id });
  };

  // 催办接口与逻辑
  handlePushDeal = (id) => {
    const { dispatch } = this.props;

    dispatch({
      type: "FlowHandle/remind",
      payload: { processInstanceId: id },
    }).then(({ errCode, errMsg }) => {
      if (errCode === 0) {
        this.openNotificationWithIcon("success", "", "催办成功");
        this.initData();
      } else {
        this.openNotificationWithIcon("error", "", errMsg || "操作失败");
      }
    });
  };

  resetPageData = () => {
    const { dispatch } = this.props;
    this.searchParams = emptyParams;
    if (this.tableForm) {
      this.tableForm.handleReset();
    }
    // dispatch({ type: "FlowHandle/clearData" });
    dispatch({
      type: "FlowHandle/set/applyData",
      payload: { datas: [], totalCount: 0 },
    });
    this.setState(
      {
        page: { pageNum: 1, pageSize: 10 },
      },
      () => {
        this.initData();
      },
    );
  };

  initData = () => {
    const { page } = this.state;
    const { dispatch } = this.props;
    const params = { ...page, ...this.searchParams };
    dispatch({ type: "FlowHandle/getProApplyNew", payload: params });
  };

  handleSearch = (values, reset) => {
    const { page } = this.state;
    this.searchParams = reset ? emptyParams : values;
    this.setState({ page: { ...page, pageNum: 1 } }, () => {
      this.initData();
    });
  };

  // 签收
  handleTaskclaim = (record = {}) => {
    const { dispatch } = this.props;
    const { taskId } = record;
    dispatch({ type: "FlowHandle/handleTaskclaim", payload: { taskId } }).then(
      ({ errCode }) => {
        if (errCode === 0) {
          this.handleSearch(emptyParams);
        }
      },
    );
  };

  // 页码页数改变
  handlePageChange = (pageNum, pageSize) => {
    this.setState({ page: { pageNum, pageSize } }, () => {
      this.initData();
    });
  };

  render() {
    const { returnBackVisible, id } = this.state;
    const { dispatch, loading } = this.props;

    const confirmLoading = loading.effects["FlowHandle/recall"];

    return (
      <>
        <SearchTable
          ref={(r) => {
            if (r) {
              this.tableForm = r;
            }
          }}
          formConfig={this.getFormConfig()}
          handleSearch={this.handleSearch}
          tableProps={this.getAntTableProps()}
        />
        {returnBackVisible && (
          <ReturnBackForm
            visible={returnBackVisible}
            confirmLoading={confirmLoading}
            onCancel={this.handleReturnBackVisible}
            dispatch={dispatch}
            processInstanceId={id}
            getList={this.initData}
          />
        )}
      </>
    );
  }
}
